const { _electron: electron } = require("playwright");

async function testClickExamples() {
    let electronApp;
    try {
        electronApp = await electron.launch({
            executablePath: "C:\\Program Files\\Max-Optics\\MaxOpticsStudio\\2025R1\\v3.3.3.0_09100454\\gui\\launcher\\MaxOpticsLauncher.exe"
        });

        // 等待一段时间让窗口完全启动
        console.log("等待 Launcher 启动...");
        await new Promise(r => setTimeout(r, 15000));

        // 获取所有窗口
        const windows = electronApp.windows();
        if (windows.length === 0) {
            throw new Error("未捕获到任何窗口，请检查应用是否正常启动。");
        }

        console.log("=== 调试模式: 已捕获的窗口 ===");
        for (const [i, page] of windows.entries()) {
            const t = await page.title();
            console.log(`[${i}] title="${t}" url=${page.url()}`);
        }
        console.log("=============================");

        // 假定最后一个窗口是 Launcher
        const launcherWindow = windows[windows.length - 1];
        console.log("假定 Launcher 窗口:", await launcherWindow.title());

        const page = launcherWindow;

        // 等待并点击 Examples
        const examplesButton = page.getByText("Examples", { exact: true });
        await examplesButton.waitFor({ state: "visible", timeout: 15000 });
        await examplesButton.click();
        console.log("已点击 Examples 按钮");

        await page.waitForTimeout(5000);

    } catch (err) {
        console.error("脚本出错:", err);
    } finally {
        if (electronApp) {
            console.log("关闭 Electron 应用...");
            await electronApp.close();
        }
    }
}

testClickExamples().then(() => {
    console.log("脚本执行完成，准备退出。");
    process.exit(0);
});
